import { $getAttributeGroupList, $getTripPlaces } from "@/api/common"
import { $getArticleTags } from "@/api/feed/note"
import { $getPublicPageWithResort } from "@/api/hy/scenic"
import type { Params } from "@/types/common"

//获取公共景区数据
export const getPublicPageWithResort = () => {
	return new Promise((resolve) => {
		$getPublicPageWithResort()
			.then((res: any) => {
				let areaResorts = [
					{
						id: "-1",
						name: "全部区域",
						resorts: [{ id: "-1", name: "不限区域" }],
					},
				]
				if (res.count > 0) {
					res.list.forEach((item: any) => {
						areaResorts.push({
							id: item.id,
							name: item.dest_name,
							resorts: item.resorts,
						})
					})
				}
				resolve(areaResorts)
			})
			.catch((err) => {
				resolve([])
			})
	})
}

//资源属性分组标签
export const getAttributeGroupList = (params: any) => {
	return new Promise((resolve) => {
		$getAttributeGroupList(params)
			.then((res: any) => {
				let attrList: any = []
				if (res.count > 0) {
					res.list.forEach((item: any) => {
						attrList.push({
							id: item.id,
							groupName: item.name,
							items: item.attrs.map((attr: any) => {
								return {
									id: attr.id,
									name: attr.name,
									value: attr.id,
								}
							}),
						})
					})
				}
				resolve(attrList)
			})
			.catch((err) => {
				resolve([])
			})
	})
}

//文章分组标签
export const getArticleTags = () => {
	return new Promise((resolve) => {
		$getArticleTags()
			.then((res: any) => {
				let attrList: any = []
				if (res.count > 0) {
					res.list.forEach((item: any) => {
						attrList.push({
							id: item.id,
							groupName: item.name,
							items: item.tags.map((attr: any) => {
								return {
									id: attr.id,
									name: attr.name,
									value: attr.id,
								}
							}),
						})
					})
				}
				resolve(attrList)
			})
			.catch((err) => {
				resolve([])
			})
	})
}

//线路出发地
export const getTripStartPlaces = () => {
	return new Promise((resolve) => {
		$getTripPlaces({ type: 10 })
			.then((res: any) => {
				let listData: any = [
					{
						id: "-1",
						name: "全部出发地",
						all: true,
						value: "-1",
					},
				]
				if (res.count > 0) {
					res.list.forEach((item: any) => {
						listData.push({
							id: item.id,
							name: item.name,
							value: item.id,
						})
					})
				}
				resolve(listData)
			})
			.catch((err) => {
				resolve([])
			})
	})
}

//线路目的地
export const getTripEndPlaces = () => {
	return new Promise((resolve) => {
		$getTripPlaces({ type: 20 })
			.then((res: any) => {
				let listData: any = [
					{
						id: "-1",
						name: "全部目的地",
						all: true,
						value: "-1",
					},
				]
				if (res.count > 0) {
					res.list.forEach((item: any) => {
						listData.push({
							id: item.id,
							name: item.name,
							value: item.id,
						})
					})
				}
				resolve(listData)
			})
			.catch((err) => {
				resolve([])
			})
	})
}
